package com.xieyong.leetcode;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

public class Leetcode1233 {
    public static void main(String[] args) {
        Leetcode1233 leetcode1233 = new Leetcode1233();
        String[] folder = {"/a/b/c","/a/b/ca","/a/b/d"};
        List<String> strings = leetcode1233.removeSubfolders(folder);
        for (String string : strings) {
            System.out.println(string);
        }
    }


    public List<String> removeSubfolders(String[] folder) {
        Arrays.sort(folder);
        List<String> res = new ArrayList<>();
        res.add(folder[0]);
        for (int i = 1; i < folder.length; i++) {
            String pre = res.get(res.size() - 1);
            if (!folder[i].equals(pre) && !folder[i].startsWith(pre + "/")) {
                res.add(folder[i]);
            }
        }
        return res;
//        循环查找超时了
//        for (String s : folder) {
//            boolean flag = true;
//            for (String string : list) {
//                if (!s.equals(string) && s.startsWith(string + "/")) {
//                    flag = false;
//                    break;
//                }
//            }
//            if (flag) {
//                res.add(s);
//            }
//        }
    }
}
